<?php

$conn_args = array(
    'host'=>'127.0.0.1',  //rabbitmq 服务器host
    'port'=>5672,         //rabbitmq 服务器端口
    'login'=>'admin',     //登录用户
    'password'=>'blldxt',   //登录密码
    'vhost'=>'/'         //虚拟主机
);
$exArr = array(
    'name' => 'myexs'
);
$conn = new \AMQPConnection($conn_args);
$conn->connect();
$channel = new \AMQPChannel($conn);  // 信道
$ex = new \AMQPExchange($channel);  // 交换机
$ex->setName($exArr['name']);
$ex->setType(AMQP_EX_TYPE_DIRECT);
$ex->setFlags(AMQP_DURABLE);
echo "Exchange Status:".$ex->declare()."\n";

$q = new \AMQPQueue($channel);
$q->setName('myqs');
$q->setFlags(AMQP_DURABLE); //持久化
echo "Message Total:".$q->declare()."\n";
echo 'Queue Bind: '.$q->bind($exArr['name'], 'myqs')."\n";
echo "Message:\n";
while(True){
    // $q->consume('processMessage');
    $q->consume('processMessage', AMQP_AUTOACK); //自动ACK应答
}
$conn->disconnect();

function processMessage($envelope, $queue) {
    $msg = $envelope->getBody();
    echo $msg."\n"; //处理消息
    // $queue->ack($envelope->getDeliveryTag()); //手动发送ACK应答
}